﻿using System;
using System.Collections.Generic;
using System.Linq;
using DLM.Domain.Model;
using DLM.Domain.Repositories;
using DLM.Infrastructure.UnitOfWork;
using DLM.Infrastructure.Domain;
using Webdiyer.WebControls.Mvc;
using DLM.Domain.DTO;

namespace DLM.Data.EF.CodeFirst.Repositories
{
    public class PartyRepository : Repository<Party, int>, IPartyRepository
    {
        public PartyRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {

        }

        public new System.Collections.IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Party> FindAll()
        {
            
            return this.ActiveContext.Party.AsNoTracking().OrderByDescending(p => p.PartyDate.Value).ToList();
        }

        public override IEnumerable<Party> FindAll(System.Linq.Expressions.Expression<Func<Party, bool>> query)
        {
            return this.ActiveContext.Party.AsNoTracking().Where(query).OrderByDescending(p => p.PartyDate.Value).ToList();
        }

        public override IEnumerable<Party> FindAll(System.Linq.Expressions.Expression<Func<Party, bool>> query, int index, int count)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("Index cannot be negative!");
            }
            return this.ActiveContext.Party.Where(query)
                .OrderByDescending(p => p.PartyDate.Value)
                .ToPagedList(index, count);
        }

        public IEnumerable<Party> FindByType(PartyType type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("Type cannot be null!");
            }
            return this.ActiveContext.Party.AsNoTracking()
                .Where(p => p.PartyType == (int)type)
                .OrderByDescending(p => p.PartyDate.Value)
                .ToList();
        }

        public IEnumerable<Party> FindByType(PartyType type, int count)
        {
            if (type == null)
            {
                throw new ArgumentNullException("Type cannot be null!");
            }

            return this.ActiveContext.Party.AsNoTracking()
                .Where(p => p.PartyType == (int)type)
                .Take(count)
                .OrderByDescending(p => p.PartyDate.Value)
                .ToList();
        }


        public IEnumerable<Party> FindByStatus(PartyStatus status, int pageIndex, int pageSize)
        {
            if (status == null)
            {
                throw new ArgumentNullException("Type cannot be null!");
            }
            return this.ActiveContext.Party.AsNoTracking()
                .Where(p => p.Status == (int)status)
                .OrderByDescending(p => p.PartyDate.Value)
                .ToPagedList<Party>(pageIndex, pageSize);
        }

        public IEnumerable<Party> FindByStatus(PartyStatus status, string name, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Name cannot be null!");
            }
            var result = from u in this.ActiveContext.User
                         join ua in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals ua.UserID
                         join p in this.ActiveContext.Party
                         on ua.PartyID equals p.PartyID
                         where p.Status == (int)status && u.AccountName == name
                         select p;
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList<Party>(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindMyEngaged(string name, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Name cannot be null!");
            }
            var result = from u in this.ActiveContext.User
                         join ua in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals ua.UserID
                         join p in this.ActiveContext.Party
                         on ua.PartyID equals p.PartyID
                         join ups in this.ActiveContext.UserPartyAssociation
                         on ua.PartyID equals ups.PartyID
                         join u1 in this.ActiveContext.User
                         on ups.UserID equals u1.UserID
                         where u.AccountName == name && ua.IsLead == 0
                         && ups.IsLead == 1
                         select new PartyDTO
                         {
                             UserID = ups.UserID,
                             PartyID = p.PartyID,
                             PartyDate = p.PartyDate,
                             CreateDate = p.CreateDate,
                             Location = p.Location,
                             PhotoUrl = p.PhotoUrl,
                             PublisherPhoto = u1.Photo,
                             Subject = p.Subject,
                             PartyType = p.PartyType
                         };
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList<PartyDTO>(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindMyRecentParties(string name, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Name cannot be null!");
            }
            var result = from u in this.ActiveContext.User
                         join ua in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals ua.UserID
                         join p in this.ActiveContext.Party
                         on ua.PartyID equals p.PartyID
                         join ups in this.ActiveContext.UserPartyAssociation
                         on ua.PartyID equals ups.PartyID
                         join u1 in this.ActiveContext.User
                         on ups.UserID equals u1.UserID
                         where u.AccountName == name
                         && ups.IsLead == 1
                         && p.Status != (int)PartyStatus.PendingForApproval
                         && p.Status != (int)PartyStatus.Cancel
                         select new PartyDTO
                         {
                             UserID = ups.UserID,
                             PartyID = p.PartyID,
                             PartyDate = p.PartyDate,
                             CreateDate = p.CreateDate,
                             Location = p.Location,
                             PhotoUrl = p.PhotoUrl,
                             PublisherPhoto = u1.Photo,
                             Subject = p.Subject,
                             PartyType = p.PartyType
                         };
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList<PartyDTO>(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindMyPublished(string name, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Name cannot be null!");
            }
            var result = from u in this.ActiveContext.User
                         join ua in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals ua.UserID
                         join p in this.ActiveContext.Party
                         on ua.PartyID equals p.PartyID
                         where ua.IsLead == 1 && u.AccountName == name
                         select new PartyDTO
                         {
                             UserID = u.UserID,
                             PartyID = p.PartyID,
                             PartyDate = p.PartyDate,
                             CreateDate = p.CreateDate,
                             Location = p.Location,
                             PhotoUrl = p.PhotoUrl,
                             PublisherPhoto = u.Photo,
                             Subject = p.Subject
                         };
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList<PartyDTO>(pageIndex, pageSize);
        }

        public IEnumerable<Party> FindByPublisher(string name, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Name cannot be null!");
            }
            var result = from u in this.ActiveContext.User
                         join ua in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals ua.UserID
                         join p in this.ActiveContext.Party
                         on ua.PartyID equals p.PartyID
                         where ua.IsLead == 1 && u.AccountName == name
                         select p;
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList<Party>(pageIndex, pageSize);
        }



        public bool IsExist(int partyID)
        {
            if (partyID < 0)
            {
                throw new ArgumentNullException("ID canot be negative!");
            }
            return this.ActiveContext.UserPartyAssociation.Count(p => p.PartyID == partyID) == 0 ? false : true;
        }

        public bool IsExist(string userName, int partyID)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("User name cannot be null!");
            }
            if (partyID < 0)
            {
                throw new ArgumentOutOfRangeException("Id cannot be negative!");
            }
            var result = from u in this.ActiveContext.User
                         join up in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals up.UserID
                         where u.AccountName == userName && up.PartyID == partyID
                         select up;
            return result.Count() == 0 ? false : true;
        }

        public bool IsLead(string userName, int partyID)
        {
            if (string.IsNullOrEmpty(userName))
            {
                throw new ArgumentNullException("User name cannot be null!");
            }
            if (partyID < 0)
            {
                throw new ArgumentOutOfRangeException("Id cannot be negative!");
            }
            var result = from u in this.ActiveContext.User
                         join up in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals up.UserID
                         where u.AccountName == userName
                         && up.IsLead == 1 && up.PartyID == partyID
                         select up;
            return result.Count() == 0 ? false : true;
        }


        public IEnumerable<Party> FindByType(PartyType type, PartyStatus status, int pageIndex, int pageSize)
        {
            if (pageIndex < 0 || pageSize < 0)
            {
                throw new ArgumentOutOfRangeException("Index cannot be negative!");
            }
            if (type == null || status == null)
            {
                throw new ArgumentNullException("Type or status cannot nbe null!");
            }
          
            var result = from u in this.ActiveContext.User
                         join ua in this.ActiveContext.UserPartyAssociation
                         on u.UserID equals ua.UserID
                         join p in this.ActiveContext.Party
                         on ua.PartyID equals p.PartyID
                         where p.Status == (int)status && p.PartyType == (int)type
                         && ua.IsLead == 1
                         select p;
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }


        public IEnumerable<PartyDTO> FindDTOByType(PartyType type, PartyStatus status, int pageIndex, int pageSize)
        {
            if (pageIndex < 0 || pageSize < 0)
            {
                throw new ArgumentOutOfRangeException("Page index");
            }
            var result = from p in this.ActiveContext.Party
                         join uap in this.ActiveContext.UserPartyAssociation
                         on p.PartyID equals uap.PartyID
                         join u in this.ActiveContext.User
                         on uap.UserID equals u.UserID
                         where p.PartyType == (int)type && (p.Status == (int)status 
                         || p.Status == (int)PartyStatus.Freeze)
                         && uap.IsLead == 1
                         select new PartyDTO
                         {
                             UserID = u.UserID,
                             PartyID = p.PartyID,
                             CreateDate = p.CreateDate,
                             EndDate = p.EndDate,
                             Location = p.Location,
                             PartyDate = p.PartyDate,
                             PhotoUrl = p.PhotoUrl,
                             PublisherPhoto = u.Photo,
                             Subject = p.Subject
                         };
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }


        public decimal SuccessRate(string accountName)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name!");
            }
            var result = (from u in this.ActiveContext.User
                          join upa in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals upa.UserID
                          join p in this.ActiveContext.Party
                          on upa.PartyID equals p.PartyID
                          where u.AccountName == accountName
                          select new { id = p.PartyID, issuccess = p.IsSuccess }).ToList();
            decimal rate = result.Where(p => p.issuccess == true).Count() / result.Count();
            return rate;
        }


        public PartyViewDTO FindViewDTO(int partyID)
        {
            if (partyID < 0)
            {
                throw new ArgumentOutOfRangeException("Party id");
            }
            var party = from p in this.ActiveContext.Party
                        join upa in this.ActiveContext.UserPartyAssociation
                        on p.PartyID equals upa.PartyID
                        join u in this.ActiveContext.User
                        on upa.UserID equals u.UserID
                        where p.PartyID == partyID
                        select new PartyViewDTO
                        {
                            Address = p.Address,
                            Area = p.Area,
                            Content = p.Content,
                            CreateDate = p.CreateDate,
                            EndDate = p.EndDate,
                            IsSuccess = p.IsSuccess,
                            Location = p.Location,
                            MemberCount = p.MemberCount,
                            Modality = p.Modality,
                            PartyDate = p.PartyDate,
                            PartyID = p.PartyID,
                            PartyType = p.PartyType,
                            PhotoUrl = p.PhotoUrl,
                            Status = p.Status,
                            Subject = p.Subject,
                            Phone = u.Phone,
                            PublishAccount = u.AccountName,
                            publishName = u.Name,
                            PublisherPhoto = u.Photo,
                            UserID = u.UserID,
                            Expense = p.Expense,
                            PaymentType = p.PaymentType
                        };
            return party.FirstOrDefault();
        }


        public IEnumerable<PartyDTO> FindByLocation(string location, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.Location.Contains(location)
                              && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);

        }


        public IEnumerable<PartyDTO> FindByPartyDate(DateTime partyDate, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.PartyDate.Value >= partyDate
                              && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              CreateDate = p.CreateDate,
                              EndDate = p.EndDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindByCategory(int type, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.PartyType == type
                          && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }


        public IEnumerable<PartyDTO> FindAll(string location, DateTime partyDate, int type, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.PartyType == type
                          && p.Location.Contains(location)
                          && p.PartyDate.Value > partyDate
                          && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindByOr(string location, DateTime partyDate, int type, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where (p.PartyType == type
                          || p.Location.Contains(location)
                          || p.PartyDate.Value > partyDate)
                          && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindByLocationAndType(string location, int type, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.PartyType == type
                          && p.Location.Contains(location)
                          && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindByLocationAndDate(string location, DateTime partyDate, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.Location.Contains(location)
                          && p.PartyDate.Value >= partyDate
                          && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }

        public IEnumerable<PartyDTO> FindByTypeAndDate(int type, DateTime partyDate, int pageIndex, int pageSize)
        {
            var result = (from u in this.ActiveContext.User
                          join ua in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals ua.UserID
                          join p in this.ActiveContext.Party
                          on ua.PartyID equals p.PartyID
                          where p.PartyDate.Value >= partyDate
                          && p.PartyType == type
                          && ua.IsLead == 1
                          && (p.Status == (int)PartyStatus.Open
                          || p.Status == (int)PartyStatus.Closed)
                          select new PartyDTO
                          {
                              UserID = u.UserID,
                              PartyID = p.PartyID,
                              PartyDate = p.PartyDate,
                              EndDate = p.EndDate,
                              CreateDate = p.CreateDate,
                              Location = p.Location,
                              PhotoUrl = p.PhotoUrl,
                              PublisherPhoto = u.Photo,
                              Subject = p.Subject
                          });
            return result.OrderByDescending(p => p.PartyDate.Value).ToPagedList(pageIndex, pageSize);
        }


        public IEnumerable<PartyDTO> FindMyInteret(int[] typeArray, string accountName, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name");
            }
            var list = from p in this.ActiveContext.Party
                       join up in this.ActiveContext.UserPartyAssociation
                       on p.PartyID equals up.PartyID
                       join u in this.ActiveContext.User
                       on up.UserID equals u.UserID
                       where typeArray.Contains(p.PartyType)
                       && up.IsLead == 1
                       && u.AccountName != accountName
                       && up.UserID != u.UserID
                       select new PartyDTO
                       {
                           UserID = up.UserID,
                           PartyID = p.PartyID,
                           PartyDate = p.PartyDate,
                           EndDate = p.EndDate,
                           CreateDate = p.CreateDate,
                           Location = p.Location,
                           PhotoUrl = p.PhotoUrl,
                           PublisherPhoto = u.Photo,
                           Subject = p.Subject
                       };
            return list.OrderByDescending(p => p.PartyDate.Value).ToPagedList<PartyDTO>(pageIndex, pageSize);
        }


        public IEnumerable<ParticipantProportion> FindParticipantProportion(int partyID)
        {
            if (partyID < 0)
            {
                throw new ArgumentOutOfRangeException("party id");
            }
            var result = from p in this.ActiveContext.Party
                         join up in this.ActiveContext.UserPartyAssociation
                         on p.PartyID equals up.PartyID
                         join u in this.ActiveContext.User
                         on up.UserID equals u.UserID
                         where p.PartyID == partyID
                         group new { u } by new { u.Gender } into r
                         select new ParticipantProportion
                         {
                             Gender = r.Key.Gender.Value,
                             Count = r.Select(u => u.u.UserID).Distinct().Count()
                         };
            return result;


        }


        public IEnumerable<AccountStatisticsDTO> Statistics()
        {
            return this.ActiveContext.Database.SqlQuery<AccountStatisticsDTO>("select engaged.userid , engaged.EngagedCount,breakapp.BreakAppCount from (select userid,count(partyid) as BreakAppCount from dbo.BreakAppointment group by userid)  breakapp join  (select userid, count(up.partyid) as EngagedCount from UserPartyAssociation up join party p on p.partyid=up.partyid where (p.status = 1 or p.status = 2) group by userid)  engaged on breakapp.userid=engaged.userid").ToList();
        }

        public int[] LeadStatistics(string accountName)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name!");
            }
            var result = (from u in this.ActiveContext.User
                          join upa in this.ActiveContext.UserPartyAssociation
                          on u.UserID equals upa.UserID
                          join p in this.ActiveContext.Party
                          on upa.PartyID equals p.PartyID
                          where u.AccountName == accountName
                          && upa.IsLead == 1
                          && p.Status == (int)PartyStatus.Closed
                          select new { id = p.PartyID, issuccess = p.IsSuccess }).ToList();

            int[] statistics = { result.Count(),result.Count(u=>u.issuccess == true) };
            return statistics;
        }


        public IEnumerable<Party> FindByDate(DateTime engagedDate, int userID)
        {
            var result = from up in this.ActiveContext.UserPartyAssociation.AsNoTracking()
                         join p in this.ActiveContext.Party.AsNoTracking()
                         on up.PartyID equals p.PartyID
                         where up.UserID == userID 
                         && p.PartyDate.Value.Day == engagedDate.Day
                         && p.Status != (int)PartyStatus.Cancel 
                         && p.Status != (int)PartyStatus.Closed
                         && p.Status !=(int)PartyStatus.Refuse
                         select p;
            return result.ToList();
        }
    }
}
